import { HttpClient, HttpParams, HttpErrorResponse, HttpHeaders } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { StorageProvider } from '../storage/storage';
import { SELECT_VALUE_ACCESSOR } from '@angular/forms/src/directives/select_control_value_accessor';

/*
  Generated class for the ApiProvider provider.

  See https://angular.io/guide/dependency-injection for more info on providers
  and Angular DI.
*/
@Injectable()
export class ApiProvider {
  private separator = '?';
  private userInfo: any;
  private data: any;
  private server_url:string;

  constructor(public http: HttpClient, storage: StorageProvider) {
    this.userInfo = storage.getObject('userinfo');
    this.server_url = storage.get('server_url');
  }

  /**
   * 查询列表数据
   * @param queryFunid 
   * @param options 
   */
  public getList(queryFunid: string, options?: any){
    let eventCode = 'query_data';
    let funId = 'queryevent';
    let params = 'funid='+funId+'&eventcode='+eventCode+'&query_funid='+queryFunid+'&has_page=0&user_id='+this.userInfo.user_id;
    if(options.where_sql){
      params += '&where_sql='+encodeURI(options.where_sql)+'&where_type='+options.where_type+'&where_value='+encodeURI(options.where_value);
    }
    return this.http.get(this.server_url+this.separator+params); 
  }

  /**
   * 查询列表数据(分页)
   * @param queryFunid 
   * @param options 
   */
  public getListPage(queryFunid: string, options: any){
    //if(!options.start||!options.limit) return;

    let eventCode = 'query_data';
    let funId = 'queryevent';
    let params = 'funid='+funId+'&eventcode='+eventCode+'&query_funid='+queryFunid+'&has_page=1&user_id='+this.userInfo.user_id;
        params += '&start='+options.start+'&limit='+options.limit;
    if(options.where_sql){
      params += '&where_sql='+encodeURI(options.where_sql)+'&where_type='+options.where_type+'&where_value='+encodeURI(options.where_value);
    } 
    return this.http.get(this.server_url+this.separator+params);   
  }

  
  formSave(formData: any){
    formData.eventcode = 'create';
    formData.pagetype = 'form';
    formData.tmpkeyid = '1';
    formData.appid = 'jxstar';
    formData.user_id = this.userInfo.user_id;
    return this.http.post(this.server_url,this.encodeHttpParams(formData));   
  }

  formUpdate(){

  }

  gridSave(urlParams: any){
    urlParams += '&eventcode=save_eg&user_id='+this.userInfo.user_id;
     return this.http.get(this.server_url+'?'+urlParams);
  }
  
  formAudit(funid:string,keyid:string){
    return this.http.post(this.server_url,this.encodeHttpParams({
      funid: funid,
      keyid: keyid,
      user_id: this.userInfo.user_id,
      eventcode: 'audit',
      auditvalue: '1',
      pagetype: 'form',
      appid: 'jxstar'
    }));
  }

request(params:any){
    params.user_id = this.userInfo.user_id;
    params.usertoken = this.userInfo.user_token;
    return this.http.post(this.server_url,this.encodeHttpParams(params));
  }

 private encodeHttpParams(params: any): any {
    if (!params) return null;
    return new HttpParams({fromObject: params});
  }
  

}

